package model
{
	import mx.collections.IList;
	import mx.utils.ObjectProxy;
	
	import visualization.DataPoint;
	import visualization.IDataPoint;
	import visualization.Visualization;
	import visualization.renderers.VisualizationRenderer;
	
	public class Model extends Field
	{
		/*====================
		 * Variables
		 *====================
		 */
		private var _isBranch:Boolean = false;
		public function get isBranch():Boolean{
			return this._isBranch;
		} 
		
		public var metrics:Array = new Array();
		
		private var _fields:Array = new Array();
		public function get fields():Array{
			return this._fields;
		}
		public function set fields(arr:Array):void{
			this._fields = arr;
			for each(var field:Object in this._fields){
				if(field is Model){
					this._isBranch = true;
				}
			}
		}
		
		public var renderer:VisualizationRenderer;
		
		/*====================
		 * Constructor
		 *====================
		 */
		public function Model()
		{
			super();
		}
		
		/*====================
		 * Methods
		 *====================
		 */
		 public function generatePoint( item:Object ):IDataPoint{
		 	var point:IDataPoint;
		 	if(this._isBranch){
		 		point = new Visualization();
		 	}
		 	else{
		 		point = new DataPoint();
		 	}
		 	
		 	point.model = this;
		 	point.title = this.title;
		 	point.fields = this.fields;
		 	point.source = item;
		 	
		 	for each(var field:Field in this.fields){
		 		if(field is Model){
		 			var collection:Object = field.getValue(item);
		 			
		 			(point as Visualization).metrics = (field as Model).metrics;
		 			(point as Visualization).renderer = (field as Model).renderer;
		 			point.model = field as Model;
		 			
		 			if(collection is IList){
		 				(point as Visualization).dataSource = collection as IList;
		 			}
		 			else if(collection is Array){
		 				(point as Visualization).addItems(collection as Array);
		 			}
		 			else{
		 				for each(var obj:Object in collection){
		 					(point as Visualization).addItem(obj);
		 				}
		 			}
		 		}
		 	}
		 	return point;
		 }
		 
	}
}